﻿Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.Drawing.Text
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Workflow.Interop

Namespace System.Workflow.ComponentModel.Design
    Public Class ActivityDesignerPaint
        ' Methods
        Shared Sub New()
            Dim colorArray(,) As Color(0 To .,0 To .) = New Color(3  - 1, 5  - 1) {}
            colorArray(0, 0) = Color.FromArgb(0, 60, &HA5)
            colorArray(0, 1) = Color.FromArgb(&HFF, &HFF, &HFF)
            colorArray(0, 2) = Color.FromArgb(&HB5, &HBA, &HD6)
            colorArray(0, 3) = Color.FromArgb(&H42, &H8E, &HFF)
            colorArray(0, 4) = Color.FromArgb(&HB5, &HC3, &HE7)
            colorArray(1, 0) = Color.FromArgb(&H31, &H44, &H73)
            colorArray(1, 1) = Color.FromArgb(&HFF, &HFF, &HFF)
            colorArray(1, 2) = Color.FromArgb(&HBA, &HBB, &HC9)
            colorArray(1, 3) = Color.FromArgb(&H7E, &H7C, &H7C)
            colorArray(1, 4) = Color.FromArgb(&HCE, &HCF, &HD8)
            colorArray(2, 0) = Color.FromArgb(&H56, &H66, &H2D)
            colorArray(2, 1) = Color.FromArgb(&HFF, &HFF, &HFF)
            colorArray(2, 2) = Color.FromArgb(210, &HDB, &HC5)
            colorArray(2, 3) = Color.FromArgb(&H72, &H92, &H1D)
            colorArray(2, 4) = Color.FromArgb(&HD4, 220, 190)
            ActivityDesignerPaint.XPColorSchemes = colorArray
        End Sub

        Public Shared Sub Draw3DButton(ByVal graphics As Graphics, ByVal image As Image, ByVal bounds As Rectangle, ByVal transparency As Single, ByVal buttonState As ButtonState)
            If (graphics Is Nothing) Then
                Throw New ArgumentNullException("graphics")
            End If
            Dim alpha As Integer = Math.Max(0, Convert.ToInt32(CSng((transparency * 255!))))
            [Using] brush As SolidBrush = New SolidBrush(Color.FromArgb(alpha, SystemColors.Control))
                [Using] pen As Pen = New Pen(Color.FromArgb(alpha, SystemColors.ControlLightLight))
                    [Using] pen2 As Pen = New Pen(Color.FromArgb(alpha, SystemColors.ControlDark))
                        [Using] pen3 As Pen = New Pen(Color.FromArgb(alpha, SystemColors.ControlDarkDark))
                            graphics.FillRectangle(brush, bounds)
                            If ((buttonState = ButtonState.Normal) OrElse (buttonState = ButtonState.Inactive)) Then
                                graphics.DrawLine(pen, CInt((bounds.Left + 1)), CInt((bounds.Bottom - 1)), CInt((bounds.Left + 1)), CInt((bounds.Top + 1)))
                                graphics.DrawLine(pen, CInt((bounds.Left + 1)), CInt((bounds.Top + 1)), CInt((bounds.Right - 1)), CInt((bounds.Top + 1)))
                                graphics.DrawLine(pen2, CInt((bounds.Left + 1)), CInt((bounds.Bottom - 1)), CInt((bounds.Right - 1)), CInt((bounds.Bottom - 1)))
                                graphics.DrawLine(pen2, CInt((bounds.Right - 1)), CInt((bounds.Bottom - 1)), CInt((bounds.Right - 1)), CInt((bounds.Top + 1)))
                                graphics.DrawLine(pen3, bounds.Left, bounds.Bottom, bounds.Right, bounds.Bottom)
                                graphics.DrawLine(pen3, bounds.Right, bounds.Bottom, bounds.Right, bounds.Top)
                            ElseIf (buttonState = ButtonState.Pushed) Then
                                graphics.DrawRectangle(pen2, bounds)
                                bounds.Offset(1, 1)
                            End If
                            If (Not image Is Nothing) Then
                                bounds.Inflate(-2, -2)
                                ActivityDesignerPaint.DrawImage(graphics, image, bounds, New Rectangle(Point.Empty, image.Size), DesignerContentAlignment.Fill, transparency, (buttonState = ButtonState.Inactive))
                            End If
                        End [Using]
                    End [Using]
                End [Using]
            End [Using]
        End Sub

        Friend Shared Sub DrawConnectors(ByVal graphics As Graphics, ByVal pen As Pen, ByVal points As Point(), ByVal connectorCapSize As Size, ByVal maxCapSize As Size, ByVal startConnectorCap As LineAnchor, ByVal endConnectorCap As LineAnchor)
            If (points.GetLength(0) >= 2) Then
                Dim path As GraphicsPath = Nothing
                Dim capinset As Single = 0!
                If (startConnectorCap <> LineAnchor.None) Then
                    Dim pointArray As Point() = New Point() { points(0), points(1) }
                    Dim num2 As Integer = If((pointArray(0).Y = pointArray(1).Y), connectorCapSize.Width, connectorCapSize.Height)
                    num2 = (num2 + (num2 Mod 2))
                    num2 = Math.Min(Math.Min(num2, maxCapSize.Width), maxCapSize.Height)
                    path = ActivityDesignerPaint.GetLineCap(startConnectorCap, num2, capinset)
                    If (((Not path Is Nothing) AndAlso ((startConnectorCap Mod LineAnchor.ArrowAnchor) = LineAnchor.None)) AndAlso ((pointArray(0).X = pointArray(1).X) OrElse (pointArray(0).Y = pointArray(1).Y))) Then
                        Dim transform As Matrix = graphics.Transform
                        graphics.TranslateTransform(CSng(pointArray(0).X), CSng(pointArray(0).Y))
                        If (pointArray(0).Y = pointArray(1).Y) Then
                            graphics.RotateTransform(If((pointArray(0).X < pointArray(1).X), 90!, 270!))
                        Else
                            graphics.RotateTransform(If((pointArray(0).Y < pointArray(1).Y), 180!, 0!))
                        End If
                        [Using] brush As Brush = New SolidBrush(pen.Color)
                            graphics.FillPath(brush, path)
                            graphics.DrawPath(pen, path)
                        End [Using]
                        graphics.Transform = If((Not transform Is Nothing), transform, New Matrix)
                    End If
                End If
                Dim path2 As GraphicsPath = Nothing
                Dim num3 As Single = 0!
                If (endConnectorCap <> LineAnchor.None) Then
                    Dim pointArray2 As Point() = New Point() { points((points.GetLength(0) - 2)), points((points.GetLength(0) - 1)) }
                    Dim num4 As Integer = If((pointArray2(0).Y = pointArray2(1).Y), connectorCapSize.Width, connectorCapSize.Height)
                    num4 = (num4 + (num4 Mod 2))
                    num4 = Math.Min(Math.Min(num4, maxCapSize.Width), maxCapSize.Height)
                    path2 = ActivityDesignerPaint.GetLineCap(endConnectorCap, num4, num3)
                    If (((Not path2 Is Nothing) AndAlso ((endConnectorCap Mod LineAnchor.ArrowAnchor) = LineAnchor.None)) AndAlso ((pointArray2(0).X = pointArray2(1).X) OrElse (pointArray2(0).Y = pointArray2(1).Y))) Then
                        Dim matrix2 As Matrix = graphics.Transform
                        graphics.TranslateTransform(CSng(pointArray2(1).X), CSng(pointArray2(1).Y))
                        If (pointArray2(0).Y = pointArray2(1).Y) Then
                            graphics.RotateTransform(If((pointArray2(0).X < pointArray2(1).X), 270!, 90!))
                        Else
                            graphics.RotateTransform(If((pointArray2(0).Y < pointArray2(1).Y), 0!, 180!))
                        End If
                        [Using] brush2 As Brush = New SolidBrush(pen.Color)
                            graphics.FillPath(brush2, path2)
                            graphics.DrawPath(pen, path2)
                        End [Using]
                        graphics.Transform = If((Not matrix2 Is Nothing), matrix2, New Matrix)
                    End If
                End If
                If (Not path Is Nothing) Then
                    Dim cap As New CustomLineCap(Nothing, path) { _
                        .WidthScale = (1! / pen.Width), _
                        .BaseInset = capinset _
                    }
                    pen.CustomStartCap = cap
                End If
                If (Not path2 Is Nothing) Then
                    Dim cap2 As New CustomLineCap(Nothing, path2) { _
                        .WidthScale = (1! / pen.Width), _
                        .BaseInset = num3 _
                    }
                    pen.CustomEndCap = cap2
                End If
                graphics.DrawLines(pen, points)
                If (Not path Is Nothing) Then
                    Dim customStartCap As CustomLineCap = pen.CustomStartCap
                    pen.StartCap = LineCap.Flat
                    customStartCap.Dispose
                End If
                If (Not path2 Is Nothing) Then
                    Dim customEndCap As CustomLineCap = pen.CustomEndCap
                    pen.EndCap = LineCap.Flat
                    customEndCap.Dispose
                End If
            End If
        End Sub

        Friend Shared Sub DrawDesignerBackground(ByVal graphics As Graphics, ByVal designer As ActivityDesigner)
            Dim designerTheme As ActivityDesignerTheme = designer.DesignerTheme
            Dim theme2 As CompositeDesignerTheme = [TryCast](designerTheme,CompositeDesignerTheme)
            Dim bounds As Rectangle = designer.Bounds
            Dim location As Point = bounds.Location
            bounds.Location = Point.Empty
            Dim transform As Matrix = graphics.Transform
            graphics.TranslateTransform(CSng(location.X), CSng(location.Y))
            Dim path As GraphicsPath = ActivityDesignerPaint.GetDesignerPath(designer, New Point(-location.X, -location.Y), Size.Empty, DesignerEdges.None)
            Dim ef As RectangleF = path.GetBounds
            Dim rectangle As New Rectangle(0, 0, Convert.ToInt32(Math.Ceiling(CDbl(ef.Width))), Convert.ToInt32(Math.Ceiling(CDbl(ef.Height))))
            graphics.FillPath(designerTheme.GetBackgroundBrush(rectangle), path)
            Dim flag As Boolean = If(TypeOf designer Is CompositeActivityDesigner, DirectCast(designer, CompositeActivityDesigner).Expanded, False)
            If (((Not theme2 Is Nothing) AndAlso flag) AndAlso (Not theme2.WatermarkImage Is Nothing)) Then
                Dim margin As Size = WorkflowTheme.CurrentTheme.AmbientTheme.Margin
                rectangle.Inflate(-margin.Width, -margin.Height)
                ActivityDesignerPaint.DrawImage(graphics, theme2.WatermarkImage, rectangle, New Rectangle(Point.Empty, theme2.WatermarkImage.Size), theme2.WatermarkAlignment, 0.25!, False)
            End If
            If WorkflowTheme.CurrentTheme.AmbientTheme.ShowDesignerBorder Then
                graphics.DrawPath(designerTheme.BorderPen, path)
            End If
            path.Dispose
            graphics.Transform = transform
        End Sub

        Friend Shared Sub DrawDropShadow(ByVal graphics As Graphics, ByVal shadowSourceRectangle As Rectangle, ByVal baseColor As Color, ByVal shadowDepth As Integer, ByVal lightSourcePosition As LightSourcePosition, ByVal lightSourceIntensity As Single, ByVal roundEdges As Boolean)
            If (graphics Is Nothing) Then
                Throw New ArgumentNullException("graphics")
            End If
            If ((shadowSourceRectangle.IsEmpty OrElse (shadowSourceRectangle.Width < 0)) OrElse (shadowSourceRectangle.Height < 0)) Then
                Throw New ArgumentException(SR.GetString("Error_InvalidShadowRectangle"), "shadowRectangle")
            End If
            If ((shadowDepth < 1) OrElse (shadowDepth > 12)) Then
                Throw New ArgumentException(SR.GetString("Error_InvalidShadowDepth"), "shadowDepth")
            End If
            If ((lightSourceIntensity <= 0!) OrElse (lightSourceIntensity > 1!)) Then
                Throw New ArgumentException(SR.GetString("Error_InvalidLightSource"), "lightSourceIntensity")
            End If
            Dim rectangle As Rectangle = shadowSourceRectangle
            Dim empty As Size = Size.Empty
            If ((lightSourcePosition And LightSourcePosition.Center) > 0) Then
                rectangle.Inflate(shadowDepth, shadowDepth)
            End If
            If ((lightSourcePosition And LightSourcePosition.Left) > 0) Then
                empty.Width = (empty.Width + (shadowDepth + 1))
            ElseIf ((lightSourcePosition And LightSourcePosition.Right) > 0) Then
                empty.Width = (empty.Width - (shadowDepth + 1))
            End If
            If ((lightSourcePosition And LightSourcePosition.Top) > 0) Then
                empty.Height = (empty.Height + (shadowDepth + 1))
            ElseIf ((lightSourcePosition And LightSourcePosition.Bottom) > 0) Then
                empty.Height = (empty.Height - (shadowDepth + 1))
            End If
            rectangle.Offset(empty.Width, empty.Height)
            Dim container As GraphicsContainer = graphics.BeginContainer
            Dim path As New GraphicsPath
            If roundEdges Then
                path.AddPath(ActivityDesignerPaint.GetRoundedRectanglePath(shadowSourceRectangle, 8), True)
            Else
                path.AddRectangle(shadowSourceRectangle)
            End If
            Try 
                [Using] region As Region = New Region(path)
                    graphics.SmoothingMode = SmoothingMode.AntiAlias
                    graphics.ExcludeClip([region])
                    Dim color As Color = Color.FromArgb(Convert.ToInt32(CSng((40! * lightSourceIntensity))), baseColor)
                    Dim num As Integer = Math.Max((40 / shadowDepth), 2)
                    Dim i As Integer
                    For i = 0 To shadowDepth - 1
                        rectangle.Inflate(-1, -1)
                        [Using] brush As Brush = New SolidBrush(color)
                            [Using] path2 As GraphicsPath = New GraphicsPath
                                If roundEdges Then
                                    path2.AddPath(ActivityDesignerPaint.GetRoundedRectanglePath(rectangle, 8), True)
                                Else
                                    path2.AddRectangle(rectangle)
                                End If
                                graphics.FillPath(brush, path2)
                            End [Using]
                        End [Using]
                        color = Color.FromArgb((color.A + num), color.R, color.G, color.B)
                    Next i
                End [Using]
            Finally
                graphics.EndContainer(container)
            End Try
        End Sub

        Public Shared Sub DrawExpandButton(ByVal graphics As Graphics, ByVal boundingRect As Rectangle, ByVal drawExpanded As Boolean, ByVal compositeDesignerTheme As CompositeDesignerTheme)
            If (graphics Is Nothing) Then
                Throw New ArgumentNullException("graphics")
            End If
            If (compositeDesignerTheme Is Nothing) Then
                Throw New ArgumentNullException("compositeDesignerTheme")
            End If
            If Not boundingRect.IsEmpty Then
                graphics.FillRectangle(compositeDesignerTheme.GetExpandButtonBackgroundBrush(boundingRect), boundingRect)
                graphics.DrawRectangle(CompositeDesignerTheme.ExpandButtonBorderPen, boundingRect)
                graphics.DrawLine(CompositeDesignerTheme.ExpandButtonForegoundPen, CInt((boundingRect.Left + 2)), CInt((boundingRect.Top + (boundingRect.Height / 2))), CInt((boundingRect.Right - 2)), CInt((boundingRect.Top + (boundingRect.Height / 2))))
                If drawExpanded Then
                    graphics.DrawLine(CompositeDesignerTheme.ExpandButtonForegoundPen, CInt((boundingRect.Left + (boundingRect.Width / 2))), CInt((boundingRect.Top + 2)), CInt((boundingRect.Left + (boundingRect.Width / 2))), CInt((boundingRect.Bottom - 2)))
                End If
            End If
        End Sub

        Friend Shared Sub DrawGrabHandles(ByVal graphics As Graphics, ByVal grabHandles As Rectangle(), ByVal isPrimary As Boolean)
            Dim rectangle As Rectangle
            For Each rectangle In grabHandles
                If isPrimary Then
                    graphics.FillRectangle(Brushes.White, rectangle)
                    graphics.DrawRectangle(WorkflowTheme.CurrentTheme.AmbientTheme.SelectionForegroundPen, rectangle)
                Else
                    Dim selectionPatternPen As Pen = WorkflowTheme.CurrentTheme.AmbientTheme.SelectionPatternPen
                    Dim dashStyle As DashStyle = selectionPatternPen.DashStyle
                    selectionPatternPen.DashStyle = DashStyle.Solid
                    graphics.FillRectangle(Brushes.White, rectangle)
                    graphics.DrawRectangle(selectionPatternPen, rectangle)
                    selectionPatternPen.DashStyle = dashStyle
                End If
            Next
        End Sub

        Friend Shared Sub DrawGrid(ByVal graphics As Graphics, ByVal viewableRectangle As Rectangle)
            Dim ambientTheme As AmbientTheme = WorkflowTheme.CurrentTheme.AmbientTheme
            If (ambientTheme.GridStyle = DashStyle.Dot) Then
                Dim empty As Point = Point.Empty
                empty.X = (viewableRectangle.X - (viewableRectangle.X Mod ambientTheme.GridSize.Width))
                empty.Y = (viewableRectangle.Y - (viewableRectangle.Y Mod ambientTheme.GridSize.Height))
                Dim i As Integer = empty.X
                Do While (i <= viewableRectangle.Right)
                    Dim j As Integer = empty.Y
                    Do While (j <= viewableRectangle.Bottom)
                        graphics.FillRectangle(ambientTheme.MajorGridBrush, New Rectangle(New Point(i, j), New Size(1, 1)))
                        If ((((i + (ambientTheme.GridSize.Width / 2)) >= viewableRectangle.Left) AndAlso ((i + (ambientTheme.GridSize.Width / 2)) <= viewableRectangle.Right)) AndAlso (((j + (ambientTheme.GridSize.Height / 2)) >= viewableRectangle.Top) AndAlso ((j + (ambientTheme.GridSize.Height / 2)) <= viewableRectangle.Bottom))) Then
                            graphics.FillRectangle(ambientTheme.MinorGridBrush, New Rectangle(New Point((i + (ambientTheme.GridSize.Width / 2)), (j + (ambientTheme.GridSize.Height / 2))), New Size(1, 1)))
                        End If
                        j = (j + Math.Max(ambientTheme.GridSize.Height, 1))
                    Loop
                    i = (i + Math.Max(ambientTheme.GridSize.Width, 1))
                Loop
            Else
                [Using] hdc As Hdc = New Hdc(graphics)
                    [Using] pen As HPen = New HPen(ambientTheme.MajorGridPen)
                        [Using] pen2 As HPen = New HPen(ambientTheme.MinorGridPen)
                            hdc.DrawGrid(pen, pen2, viewableRectangle, ambientTheme.GridSize, True)
                        End [Using]
                    End [Using]
                End [Using]
            End If
        End Sub

        Friend Shared Sub DrawImage(ByVal graphics As Graphics, ByVal image As Image, ByVal destination As Rectangle, ByVal transparency As Single)
            If (image Is Nothing) Then
                Throw New ArgumentNullException("image")
            End If
            ActivityDesignerPaint.DrawImage(graphics, image, destination, New Rectangle(Point.Empty, image.Size), DesignerContentAlignment.Center, transparency, False)
        End Sub

        Public Shared Sub DrawImage(ByVal graphics As Graphics, ByVal image As Image, ByVal destination As Rectangle, ByVal alignment As DesignerContentAlignment)
            If (image Is Nothing) Then
                Throw New ArgumentNullException("image")
            End If
            ActivityDesignerPaint.DrawImage(graphics, image, destination, New Rectangle(Point.Empty, image.Size), alignment, 1!, False)
        End Sub

        Public Shared Sub DrawImage(ByVal graphics As Graphics, ByVal image As Image, ByVal destination As Rectangle, ByVal source As Rectangle, ByVal alignment As DesignerContentAlignment, ByVal transparency As Single, ByVal grayscale As Boolean)
            If (graphics Is Nothing) Then
                Throw New ArgumentNullException("graphics")
            End If
            If (image Is Nothing) Then
                Throw New ArgumentNullException("image")
            End If
            If destination.IsEmpty Then
                Throw New ArgumentNullException("destination")
            End If
            If source.IsEmpty Then
                Throw New ArgumentNullException("source")
            End If
            If ((transparency < 0!) OrElse (transparency > 1!)) Then
                Throw New ArgumentNullException("transparency")
            End If
            Dim destRect As Rectangle = ActivityDesignerPaint.GetRectangleFromAlignment(alignment, destination, source.Size)
            If ((Not image Is Nothing) AndAlso Not destRect.IsEmpty) Then
                Dim newColorMatrix As New ColorMatrix
                If grayscale Then
                    newColorMatrix.Matrix00 = 0.3333333!
                    newColorMatrix.Matrix01 = 0.3333333!
                    newColorMatrix.Matrix02 = 0.3333333!
                    newColorMatrix.Matrix10 = 0.3333333!
                    newColorMatrix.Matrix11 = 0.3333333!
                    newColorMatrix.Matrix12 = 0.3333333!
                    newColorMatrix.Matrix20 = 0.3333333!
                    newColorMatrix.Matrix21 = 0.3333333!
                    newColorMatrix.Matrix22 = 0.3333333!
                End If
                newColorMatrix.Matrix33 = transparency
                Dim imageAttr As New ImageAttributes
                imageAttr.SetColorMatrix(newColorMatrix)
                graphics.DrawImage(image, destRect, source.X, source.Y, source.Width, source.Height, GraphicsUnit.Pixel, imageAttr)
            End If
        End Sub

        Friend Shared Sub DrawInvalidDesignerIndicator(ByVal graphics As Graphics, ByVal activityDesigner As ActivityDesigner)
            Dim bounds As Rectangle = activityDesigner.Bounds
            graphics.DrawRectangle(Pens.Red, bounds)
            graphics.DrawLine(Pens.Red, bounds.Left, bounds.Top, bounds.Right, bounds.Bottom)
            graphics.DrawLine(Pens.Red, bounds.Right, bounds.Top, bounds.Left, bounds.Bottom)
        End Sub

        Public Shared Sub DrawRoundedRectangle(ByVal graphics As Graphics, ByVal drawingPen As Pen, ByVal rectangle As Rectangle, ByVal radius As Integer)
            If (graphics Is Nothing) Then
                Throw New ArgumentNullException("graphics")
            End If
            If (drawingPen Is Nothing) Then
                Throw New ArgumentNullException("drawingPen")
            End If
            Dim roundedRectanglePath As GraphicsPath = Nothing
            roundedRectanglePath = ActivityDesignerPaint.GetRoundedRectanglePath(rectangle, (radius * 2))
            graphics.DrawPath(drawingPen, roundedRectanglePath)
            roundedRectanglePath.Dispose
        End Sub

        Friend Shared Sub DrawSelection(ByVal graphics As Graphics, ByVal boundingRect As Rectangle, ByVal isPrimary As Boolean, ByVal selectionSize As Size, ByVal grabHandles As Rectangle())
            Dim interpolationMode As InterpolationMode = graphics.InterpolationMode
            Dim smoothingMode As SmoothingMode = graphics.SmoothingMode
            graphics.InterpolationMode = InterpolationMode.High
            graphics.SmoothingMode = SmoothingMode.None
            Dim rect As Rectangle = boundingRect
            rect.Inflate(selectionSize.Width, selectionSize.Height)
            rect.Inflate((-selectionSize.Width / 2), (-selectionSize.Height / 2))
            graphics.DrawRectangle(WorkflowTheme.CurrentTheme.AmbientTheme.SelectionPatternPen, rect)
            rect.Inflate((selectionSize.Width / 2), (selectionSize.Height / 2))
            ActivityDesignerPaint.DrawGrabHandles(graphics, grabHandles, isPrimary)
            graphics.InterpolationMode = interpolationMode
            graphics.SmoothingMode = smoothingMode
        End Sub

        Public Shared Sub DrawText(ByVal graphics As Graphics, ByVal font As Font, ByVal [text] As String, ByVal boundingRect As Rectangle, ByVal alignment As StringAlignment, ByVal textQuality As TextQuality, ByVal textBrush As Brush)
            If (graphics Is Nothing) Then
                Throw New ArgumentNullException("graphics")
            End If
            If (font Is Nothing) Then
                Throw New ArgumentNullException("font")
            End If
            If ([text] Is Nothing) Then
                Throw New ArgumentNullException("text")
            End If
            If (textBrush Is Nothing) Then
                Throw New ArgumentNullException("textBrush")
            End If
            If Not boundingRect.IsEmpty Then
                Dim format As New StringFormat { _
                    .Alignment = alignment, _
                    .LineAlignment = StringAlignment.Center, _
                    .Trimming = StringTrimming.EllipsisCharacter, _
                    .FormatFlags = (StringFormatFlags.NoClip Or StringFormatFlags.FitBlackBox) _
                }
                Dim textRenderingHint As TextRenderingHint = graphics.TextRenderingHint
                graphics.TextRenderingHint = If((textQuality = TextQuality.AntiAliased), TextRenderingHint.AntiAlias, TextRenderingHint.SystemDefault)
                graphics.DrawString([text], font, textBrush, boundingRect, format)
                graphics.TextRenderingHint = textRenderingHint
            End If
        End Sub

        Friend Shared Function GetDesignerPath(ByVal designer As ActivityDesigner, ByVal enableRoundedCorners As Boolean) As GraphicsPath
            Return ActivityDesignerPaint.GetDesignerPath(designer, Point.Empty, Size.Empty, DesignerEdges.None, enableRoundedCorners)
        End Function

        Friend Shared Function GetDesignerPath(ByVal designer As ActivityDesigner, ByVal offset As Point, ByVal inflate As Size, ByVal edgeToInflate As DesignerEdges) As GraphicsPath
            Return ActivityDesignerPaint.GetDesignerPath(designer, offset, inflate, edgeToInflate, True)
        End Function

        Friend Shared Function GetDesignerPath(ByVal designer As ActivityDesigner, ByVal offset As Point, ByVal inflate As Size, ByVal edgeToInflate As DesignerEdges, ByVal enableRoundedCorners As Boolean) As GraphicsPath
            Dim path As New GraphicsPath
            Dim bounds As Rectangle = designer.Bounds
            bounds.Offset(offset)
            If ((edgeToInflate And DesignerEdges.Left) > DesignerEdges.None) Then
                bounds.X = (bounds.X - inflate.Width)
                bounds.Width = (bounds.Width + inflate.Width)
            End If
            If ((edgeToInflate And DesignerEdges.Right) > DesignerEdges.None) Then
                bounds.Width = (bounds.Width + inflate.Width)
            End If
            If ((edgeToInflate And DesignerEdges.Top) > DesignerEdges.None) Then
                bounds.Y = (bounds.Y - inflate.Height)
                bounds.Height = (bounds.Height + inflate.Height)
            End If
            If ((edgeToInflate And DesignerEdges.Bottom) > DesignerEdges.None) Then
                bounds.Height = (bounds.Height + inflate.Height)
            End If
            If ((designer Is ActivityDesigner.GetSafeRootDesigner(designer.Activity.Site)) AndAlso (DirectCast(designer, IWorkflowRootDesigner).InvokingDesigner Is Nothing)) Then
                path.AddRectangle(bounds)
                Return path
            End If
            Dim designerTheme As ActivityDesignerTheme = designer.DesignerTheme
            If ((enableRoundedCorners AndAlso (Not designerTheme Is Nothing)) AndAlso (designerTheme.DesignerGeometry = DesignerGeometry.RoundedRectangle)) Then
                path.AddPath(ActivityDesignerPaint.GetRoundedRectanglePath(bounds, 8), True)
                Return path
            End If
            path.AddRectangle(bounds)
            Return path
        End Function

        Friend Shared Function GetLineCap(ByVal lineCap As LineAnchor, ByVal capsize As Integer, <Out> ByRef capinset As Single) As GraphicsPath
            Dim num As Integer
            capinset = 0!
            capinset = capsize
            Dim size As New Size(capsize, capsize)
            Dim path As New GraphicsPath
            Select Case lineCap
                Case LineAnchor.Arrow, LineAnchor.ArrowAnchor
                    num = (size.Height / 3)
                    path.AddLine((size.Width / 2), -size.Height, 0, 0)
                    path.AddLine(0, 0, (-size.Width / 2), -size.Height)
                    path.AddLine((-size.Width / 2), -size.Height, 0, (-size.Height + num))
                    path.AddLine(0, (-size.Height + num), (size.Width / 2), -size.Height)
                    capinset = (size.Height - num)
                    Exit Select
                Case LineAnchor.Diamond, LineAnchor.DiamondAnchor
                    path.AddLine(0, -size.Height, (size.Width / 2), (-size.Height / 2))
                    path.AddLine((size.Width / 2), (-size.Height / 2), 0, 0)
                    path.AddLine(0, 0, (-size.Width / 2), (-size.Height / 2))
                    path.AddLine((-size.Width / 2), (-size.Height / 2), 0, -size.Height)
                    Exit Select
                Case LineAnchor.Round, LineAnchor.RoundAnchor
                    path.AddEllipse(New Rectangle((-size.Width / 2), -size.Height, size.Width, size.Height))
                    Exit Select
                Case LineAnchor.Rectangle, LineAnchor.RectangleAnchor
                    path.AddRectangle(New Rectangle((-size.Width / 2), -size.Height, size.Width, size.Height))
                    Exit Select
                Case LineAnchor.RoundedRectangle, LineAnchor.RoundedRectangleAnchor
                    num = (size.Height / 4)
                    path.AddPath(ActivityDesignerPaint.GetRoundedRectanglePath(New Rectangle((-size.Width / 2), -size.Height, size.Width, size.Height), num), True)
                    Exit Select
            End Select
            path.CloseFigure
            Return path
        End Function

        Friend Shared Function GetPagePaths(ByVal pageBounds As Rectangle, ByVal pageFoldSize As Integer, ByVal foldAlignment As DesignerContentAlignment) As GraphicsPath()
            Dim pathArray As GraphicsPath() = New GraphicsPath(2  - 1) {}
            If (foldAlignment = DesignerContentAlignment.TopLeft) Then
                pathArray(0) = New GraphicsPath
                pathArray(0).AddLine(pageBounds.Left, (pageBounds.Top + pageFoldSize), (pageBounds.Left + pageFoldSize), (pageBounds.Top + pageFoldSize))
                pathArray(0).AddLine((pageBounds.Left + pageFoldSize), (pageBounds.Top + pageFoldSize), (pageBounds.Left + pageFoldSize), pageBounds.Top)
                pathArray(0).AddLine((pageBounds.Left + pageFoldSize), pageBounds.Top, pageBounds.Right, pageBounds.Top)
                pathArray(0).AddLine(pageBounds.Right, pageBounds.Top, pageBounds.Right, pageBounds.Bottom)
                pathArray(0).AddLine(pageBounds.Right, pageBounds.Bottom, pageBounds.Left, pageBounds.Bottom)
                pathArray(0).AddLine(pageBounds.Left, pageBounds.Bottom, pageBounds.Left, (pageBounds.Top + pageFoldSize))
                pathArray(1) = New GraphicsPath
                pathArray(1).AddLine(pageBounds.Left, (pageBounds.Top + pageFoldSize), (pageBounds.Left + pageFoldSize), (pageBounds.Top + pageFoldSize))
                pathArray(1).AddLine((pageBounds.Left + pageFoldSize), (pageBounds.Top + pageFoldSize), (pageBounds.Left + pageFoldSize), pageBounds.Top)
                pathArray(1).AddLine((pageBounds.Left + pageFoldSize), pageBounds.Top, pageBounds.Left, (pageBounds.Top + pageFoldSize))
                Return pathArray
            End If
            If (foldAlignment = DesignerContentAlignment.BottomLeft) Then
                pathArray(0) = New GraphicsPath
                pathArray(0).AddLine(pageBounds.Left, pageBounds.Top, pageBounds.Right, pageBounds.Top)
                pathArray(0).AddLine(pageBounds.Right, pageBounds.Top, pageBounds.Right, pageBounds.Bottom)
                pathArray(0).AddLine(pageBounds.Right, pageBounds.Bottom, (pageBounds.Left + pageFoldSize), pageBounds.Bottom)
                pathArray(0).AddLine((pageBounds.Left + pageFoldSize), pageBounds.Bottom, (pageBounds.Left + pageFoldSize), (pageBounds.Bottom - pageFoldSize))
                pathArray(0).AddLine((pageBounds.Left + pageFoldSize), (pageBounds.Bottom - pageFoldSize), pageBounds.Left, (pageBounds.Bottom - pageFoldSize))
                pathArray(0).AddLine(pageBounds.Left, (pageBounds.Bottom - pageFoldSize), pageBounds.Left, pageBounds.Top)
                pathArray(1) = New GraphicsPath
                pathArray(1).AddLine((pageBounds.Left + pageFoldSize), pageBounds.Bottom, (pageBounds.Left + pageFoldSize), (pageBounds.Bottom - pageFoldSize))
                pathArray(1).AddLine((pageBounds.Left + pageFoldSize), (pageBounds.Bottom - pageFoldSize), pageBounds.Left, (pageBounds.Bottom - pageFoldSize))
                pathArray(1).AddLine(pageBounds.Left, (pageBounds.Bottom - pageFoldSize), (pageBounds.Left + pageFoldSize), pageBounds.Bottom)
                Return pathArray
            End If
            If (foldAlignment = DesignerContentAlignment.TopRight) Then
                pathArray(0) = New GraphicsPath
                pathArray(0).AddLine(pageBounds.Left, pageBounds.Top, (pageBounds.Right - pageFoldSize), pageBounds.Top)
                pathArray(0).AddLine((pageBounds.Right - pageFoldSize), pageBounds.Top, (pageBounds.Right - pageFoldSize), (pageBounds.Top + pageFoldSize))
                pathArray(0).AddLine((pageBounds.Right - pageFoldSize), (pageBounds.Top + pageFoldSize), pageBounds.Right, (pageBounds.Top + pageFoldSize))
                pathArray(0).AddLine(pageBounds.Right, (pageBounds.Top + pageFoldSize), pageBounds.Right, pageBounds.Bottom)
                pathArray(0).AddLine(pageBounds.Right, pageBounds.Bottom, pageBounds.Left, pageBounds.Bottom)
                pathArray(0).AddLine(pageBounds.Left, pageBounds.Bottom, pageBounds.Left, pageBounds.Top)
                pathArray(1) = New GraphicsPath
                pathArray(1).AddLine((pageBounds.Right - pageFoldSize), pageBounds.Top, (pageBounds.Right - pageFoldSize), (pageBounds.Top + pageFoldSize))
                pathArray(1).AddLine((pageBounds.Right - pageFoldSize), (pageBounds.Top + pageFoldSize), pageBounds.Right, (pageBounds.Top + pageFoldSize))
                pathArray(1).AddLine(pageBounds.Right, (pageBounds.Top + pageFoldSize), (pageBounds.Right - pageFoldSize), pageBounds.Top)
                Return pathArray
            End If
            If (foldAlignment = DesignerContentAlignment.BottomRight) Then
                pathArray(0) = New GraphicsPath
                pathArray(0).AddLine(pageBounds.Left, pageBounds.Top, pageBounds.Right, pageBounds.Top)
                pathArray(0).AddLine(pageBounds.Right, pageBounds.Top, pageBounds.Right, (pageBounds.Bottom - pageFoldSize))
                pathArray(0).AddLine(pageBounds.Right, (pageBounds.Bottom - pageFoldSize), (pageBounds.Right - pageFoldSize), (pageBounds.Bottom - pageFoldSize))
                pathArray(0).AddLine((pageBounds.Right - pageFoldSize), (pageBounds.Bottom - pageFoldSize), (pageBounds.Right - pageFoldSize), pageBounds.Bottom)
                pathArray(0).AddLine((pageBounds.Right - pageFoldSize), pageBounds.Bottom, pageBounds.Left, pageBounds.Bottom)
                pathArray(0).AddLine(pageBounds.Left, pageBounds.Bottom, pageBounds.Left, pageBounds.Top)
                pathArray(1) = New GraphicsPath
                pathArray(1).AddLine(pageBounds.Right, (pageBounds.Bottom - pageFoldSize), (pageBounds.Right - pageFoldSize), (pageBounds.Bottom - pageFoldSize))
                pathArray(1).AddLine((pageBounds.Right - pageFoldSize), (pageBounds.Bottom - pageFoldSize), (pageBounds.Right - pageFoldSize), pageBounds.Bottom)
                pathArray(1).AddLine((pageBounds.Right - pageFoldSize), pageBounds.Bottom, pageBounds.Right, (pageBounds.Bottom - pageFoldSize))
            End If
            Return pathArray
        End Function

        Friend Shared Function GetRectangleFromAlignment(ByVal alignment As DesignerContentAlignment, ByVal destination As Rectangle, ByVal size As Size) As Rectangle
            If (size.IsEmpty OrElse destination.IsEmpty) Then
                Return Rectangle.Empty
            End If
            Dim empty As Rectangle = Rectangle.Empty
            empty.Width = Math.Min(size.Width, destination.Width)
            empty.Height = Math.Min(size.Height, destination.Height)
            If ((alignment And DesignerContentAlignment.Fill) > DirectCast(0, DesignerContentAlignment)) Then
                Return destination
            End If
            If ((alignment And DesignerContentAlignment.Left) > DirectCast(0, DesignerContentAlignment)) Then
                empty.X = destination.Left
            ElseIf ((alignment And DesignerContentAlignment.Right) > DirectCast(0, DesignerContentAlignment)) Then
                empty.X = (destination.Right - empty.Width)
            Else
                empty.X = ((destination.Left + (destination.Width / 2)) - (empty.Width / 2))
            End If
            If ((alignment And DesignerContentAlignment.Top) > DirectCast(0, DesignerContentAlignment)) Then
                empty.Y = destination.Top
                Return empty
            End If
            If ((alignment And DesignerContentAlignment.Bottom) > DirectCast(0, DesignerContentAlignment)) Then
                empty.Y = (destination.Bottom - empty.Height)
                Return empty
            End If
            empty.Y = ((destination.Top + (destination.Height / 2)) - (empty.Height / 2))
            Return empty
        End Function

        Public Shared Function GetRoundedRectanglePath(ByVal rectangle As Rectangle, ByVal radius As Integer) As GraphicsPath
            If rectangle.IsEmpty Then
                Throw New ArgumentException(SR.GetString("Error_EmptyRectangleValue"), "rectangle")
            End If
            If (radius <= 0) Then
                Throw New ArgumentException(SR.GetString("Error_InvalidRadiusValue"), "radius")
            End If
            Dim width As Integer = (radius * 2)
            Dim path As New GraphicsPath
            path.AddLine(rectangle.Left, (rectangle.Bottom - radius), rectangle.Left, (rectangle.Top + radius))
            path.AddArc(rectangle.Left, rectangle.Top, width, width, 180!, 90!)
            path.AddLine((rectangle.Left + radius), rectangle.Top, (rectangle.Right - radius), rectangle.Top)
            path.AddArc((rectangle.Right - width), rectangle.Top, width, width, 270!, 90!)
            path.AddLine(rectangle.Right, (rectangle.Top + radius), rectangle.Right, (rectangle.Bottom - radius))
            path.AddArc((rectangle.Right - width), (rectangle.Bottom - width), width, width, 0!, 90!)
            path.AddLine((rectangle.Right - radius), rectangle.Bottom, (rectangle.Left + radius), rectangle.Bottom)
            path.AddArc(rectangle.Left, (rectangle.Bottom - width), width, width, 90!, 90!)
            path.CloseFigure
            Return path
        End Function

        Friend Shared Function GetScrollIndicatorPath(ByVal bounds As Rectangle, ByVal button As ScrollButton) As GraphicsPath
            Dim path As New GraphicsPath
            If Not bounds.IsEmpty Then
                If ((button = ScrollButton.Left) OrElse (button = ScrollButton.Right)) Then
                    Dim height As Integer = (bounds.Height + (bounds.Height Mod 2))
                    Dim num2 As Integer = (height / 2)
                    Dim size As New Size((height / 2), height)
                    If (button = ScrollButton.Right) Then
                        path.AddLine((bounds.Left + ((bounds.Width - size.Width) / 2)), bounds.Top, (bounds.Left + ((bounds.Width - size.Width) / 2)), (bounds.Top + size.Height))
                        path.AddLine(CInt((bounds.Left + ((bounds.Width - size.Width) / 2))), CInt((bounds.Top + size.Height)), CInt(((bounds.Left + ((bounds.Width - size.Width) / 2)) + size.Width)), CInt((bounds.Top + num2)))
                        path.AddLine(((bounds.Left + ((bounds.Width - size.Width) / 2)) + size.Width), (bounds.Top + num2), (bounds.Left + ((bounds.Width - size.Width) / 2)), bounds.Top)
                    Else
                        path.AddLine(CInt((bounds.Left + ((bounds.Width - size.Width) / 2))), CInt((bounds.Top + num2)), CInt(((bounds.Left + ((bounds.Width - size.Width) / 2)) + size.Width)), CInt((bounds.Top + size.Height)))
                        path.AddLine(((bounds.Left + ((bounds.Width - size.Width) / 2)) + size.Width), (bounds.Top + size.Height), ((bounds.Left + ((bounds.Width - size.Width) / 2)) + size.Width), bounds.Top)
                        path.AddLine(((bounds.Left + ((bounds.Width - size.Width) / 2)) + size.Width), bounds.Top, (bounds.Left + ((bounds.Width - size.Width) / 2)), (bounds.Top + num2))
                    End If
                ElseIf ((button = ScrollButton.Up) OrElse (button = ScrollButton.Down)) Then
                    Dim width As Integer = (bounds.Width + (bounds.Width Mod 2))
                    Dim num4 As Integer = (width / 2)
                    Dim size2 As New Size(width, (width / 2))
                    If (button = ScrollButton.Down) Then
                        path.AddLine(bounds.Left, (bounds.Top + ((bounds.Height - size2.Height) / 2)), (bounds.Left + size2.Width), (bounds.Top + ((bounds.Height - size2.Height) / 2)))
                        path.AddLine(CInt((bounds.Left + size2.Width)), CInt((bounds.Top + ((bounds.Height - size2.Height) / 2))), CInt((bounds.Left + num4)), CInt(((bounds.Top + ((bounds.Height - size2.Height) / 2)) + size2.Height)))
                        path.AddLine((bounds.Left + num4), ((bounds.Top + ((bounds.Height - size2.Height) / 2)) + size2.Height), bounds.Left, (bounds.Top + ((bounds.Height - size2.Height) / 2)))
                    Else
                        path.AddLine(CInt((bounds.Left + num4)), CInt((bounds.Top + ((bounds.Height - size2.Height) / 2))), CInt((bounds.Left + size2.Width)), CInt(((bounds.Top + ((bounds.Height - size2.Height) / 2)) + size2.Height)))
                        path.AddLine((bounds.Left + size2.Width), ((bounds.Top + ((bounds.Height - size2.Height) / 2)) + size2.Height), bounds.Left, ((bounds.Top + ((bounds.Height - size2.Height) / 2)) + size2.Height))
                        path.AddLine(bounds.Left, ((bounds.Top + ((bounds.Height - size2.Height) / 2)) + size2.Height), (bounds.Left + num4), (bounds.Top + ((bounds.Height - size2.Height) / 2)))
                    End If
                End If
            End If
            path.CloseFigure
            Return path
        End Function

        Friend Shared Function MeasureString(ByVal graphics As Graphics, ByVal font As Font, ByVal [text] As String, ByVal alignment As StringAlignment, ByVal maxSize As Size) As Size
            Dim empty As SizeF = SizeF.Empty
            If maxSize.IsEmpty Then
                empty = graphics.MeasureString([text], font)
            Else
                Dim stringFormat As New StringFormat { _
                    .Alignment = alignment, _
                    .LineAlignment = StringAlignment.Center, _
                    .Trimming = StringTrimming.EllipsisCharacter, _
                    .FormatFlags = StringFormatFlags.NoClip _
                }
                empty = graphics.MeasureString([text], font, New SizeF(CSng(maxSize.Width), CSng(maxSize.Height)), stringFormat)
            End If
            Return New Size(Convert.ToInt32(Math.Ceiling(CDbl(empty.Width))), Convert.ToInt32(Math.Ceiling(CDbl(empty.Height))))
        End Function


        ' Fields
        Private Shared XPColorSchemes As Color(0 To .,0 To .)

        ' Nested Types
        Private NotInheritable Class Hdc
            Implements IDisposable
            ' Methods
            Friend Sub New(ByVal graphics As Graphics)
                Me.graphics = graphics
                Dim xform As New XFORM(Me.graphics.Transform)
                Me.hdc = New HandleRef(Me, Me.graphics.GetHdc)
                Me.oldGraphicsMode = NativeMethods.SetGraphicsMode(Me.hdc, 2)
                If (Me.oldGraphicsMode = 0) Then
                    Throw New Win32Exception(Marshal.GetLastWin32Error)
                End If
                If (NativeMethods.SetWorldTransform(Me.hdc, xform) = 0) Then
                    Throw New Win32Exception(Marshal.GetLastWin32Error)
                End If
                Dim currentObject As IntPtr = NativeMethods.GetCurrentObject(Me.hdc, 1)
                If (currentObject = IntPtr.Zero) Then
                    Throw New Win32Exception(Marshal.GetLastWin32Error)
                End If
                Me.oldPen = New HandleRef(Me, currentObject)
                currentObject = NativeMethods.GetCurrentObject(Me.hdc, 11)
                If (currentObject = IntPtr.Zero) Then
                    Throw New Win32Exception(Marshal.GetLastWin32Error)
                End If
                Me.oldPenEx = New HandleRef(Me, currentObject)
                currentObject = NativeMethods.GetCurrentObject(Me.hdc, 2)
                If (currentObject = IntPtr.Zero) Then
                    Throw New Win32Exception(Marshal.GetLastWin32Error)
                End If
                Me.oldBrush = New HandleRef(Me, currentObject)
            End Sub

            Friend Sub DrawGrid(ByVal majorGridPen As HPen, ByVal minorGridPen As HPen, ByVal viewableRectangle As Rectangle, ByVal gridUnit As Size, ByVal showMinorGrid As Boolean)
                Try 
                    Dim empty As Point = Point.Empty
                    empty.X = (viewableRectangle.X - (viewableRectangle.X Mod gridUnit.Width))
                    empty.Y = (viewableRectangle.Y - (viewableRectangle.Y Mod gridUnit.Height))
                    If (NativeMethods.SelectObject(Me.hdc, majorGridPen.Handle) = IntPtr.Zero) Then
                        Throw New Win32Exception(Marshal.GetLastWin32Error)
                    End If
                    Dim i As Integer = empty.X
                    Do While (i <= viewableRectangle.Right)
                        If (i >= viewableRectangle.Left) Then
                            If Not NativeMethods.MoveToEx(Me.hdc, i, (viewableRectangle.Top + 1), Nothing) Then
                                Throw New Win32Exception(Marshal.GetLastWin32Error)
                            End If
                            If Not NativeMethods.LineTo(Me.hdc, i, (viewableRectangle.Bottom - 1)) Then
                                Throw New Win32Exception(Marshal.GetLastWin32Error)
                            End If
                        End If
                        If ((showMinorGrid AndAlso ((i + (gridUnit.Width / 2)) >= viewableRectangle.Left)) AndAlso ((i + (gridUnit.Width / 2)) <= viewableRectangle.Right)) Then
                            If (NativeMethods.SelectObject(Me.hdc, minorGridPen.Handle) = IntPtr.Zero) Then
                                Throw New Win32Exception(Marshal.GetLastWin32Error)
                            End If
                            If Not NativeMethods.MoveToEx(Me.hdc, (i + (gridUnit.Width / 2)), (viewableRectangle.Top + 1), Nothing) Then
                                Throw New Win32Exception(Marshal.GetLastWin32Error)
                            End If
                            If Not NativeMethods.LineTo(Me.hdc, (i + (gridUnit.Width / 2)), (viewableRectangle.Bottom - 1)) Then
                                Throw New Win32Exception(Marshal.GetLastWin32Error)
                            End If
                            If (NativeMethods.SelectObject(Me.hdc, majorGridPen.Handle) = IntPtr.Zero) Then
                                Throw New Win32Exception(Marshal.GetLastWin32Error)
                            End If
                        End If
                        i = (i + Math.Max(gridUnit.Width, 1))
                    Loop
                    Dim j As Integer = empty.Y
                    Do While (j <= viewableRectangle.Bottom)
                        If (j >= viewableRectangle.Top) Then
                            If Not NativeMethods.MoveToEx(Me.hdc, (viewableRectangle.Left + 1), j, Nothing) Then
                                Throw New Win32Exception(Marshal.GetLastWin32Error)
                            End If
                            If Not NativeMethods.LineTo(Me.hdc, (viewableRectangle.Right - 1), j) Then
                                Throw New Win32Exception(Marshal.GetLastWin32Error)
                            End If
                        End If
                        If ((showMinorGrid AndAlso ((j + (gridUnit.Height / 2)) >= viewableRectangle.Top)) AndAlso ((j + (gridUnit.Height / 2)) <= viewableRectangle.Bottom)) Then
                            If (NativeMethods.SelectObject(Me.hdc, minorGridPen.Handle) = IntPtr.Zero) Then
                                Throw New Win32Exception(Marshal.GetLastWin32Error)
                            End If
                            If Not NativeMethods.MoveToEx(Me.hdc, (viewableRectangle.Left + 1), (j + (gridUnit.Height / 2)), Nothing) Then
                                Throw New Win32Exception(Marshal.GetLastWin32Error)
                            End If
                            If Not NativeMethods.LineTo(Me.hdc, (viewableRectangle.Right - 1), (j + (gridUnit.Height / 2))) Then
                                Throw New Win32Exception(Marshal.GetLastWin32Error)
                            End If
                            If (NativeMethods.SelectObject(Me.hdc, majorGridPen.Handle) = IntPtr.Zero) Then
                                Throw New Win32Exception(Marshal.GetLastWin32Error)
                            End If
                        End If
                        j = (j + Math.Max(gridUnit.Height, 1))
                    Loop
                Finally
                    If (NativeMethods.SelectObject(Me.hdc, Me.oldPen) = IntPtr.Zero) Then
                        Throw New Win32Exception(Marshal.GetLastWin32Error)
                    End If
                End Try
            End Sub

            Private Sub System.IDisposable.Dispose() Implements IDisposable.Dispose
                If (Not Me.graphics Is Nothing) Then
                    If (NativeMethods.SelectObject(Me.hdc, Me.oldPen) = IntPtr.Zero) Then
                        Dim exception As New Win32Exception
                        Dim message As String = exception.Message
                    End If
                    If (NativeMethods.SelectObject(Me.hdc, Me.oldPenEx) = IntPtr.Zero) Then
                        Dim exception2 As New Win32Exception
                        Dim text2 As String = exception2.Message
                    End If
                    If (NativeMethods.SelectObject(Me.hdc, Me.oldBrush) = IntPtr.Zero) Then
                        Dim exception3 As New Win32Exception
                        Dim text3 As String = exception3.Message
                    End If
                    If (NativeMethods.SetWorldTransform(Me.hdc, New XFORM) = 0) Then
                        Dim exception4 As New Win32Exception
                        Dim text4 As String = exception4.Message
                    End If
                    If (NativeMethods.SetGraphicsMode(Me.hdc, Me.oldGraphicsMode) = 0) Then
                        Dim exception5 As New Win32Exception
                        Dim text5 As String = exception5.Message
                    End If
                    Me.graphics.ReleaseHdc
                    Me.graphics = Nothing
                End If
            End Sub


            ' Fields
            Private graphics As Graphics
            Private hdc As HandleRef
            Private oldBrush As HandleRef
            Private oldGraphicsMode As Integer
            Private oldPen As HandleRef
            Private oldPenEx As HandleRef
        End Class

        Private NotInheritable Class HPen
            Implements IDisposable
            ' Methods
            Friend Sub New(ByVal pen As Pen)
                Me.pen = pen
                Dim num As Integer = If((pen.DashStyle < DashStyle.DashDotDot), CInt(pen.DashStyle), 0)
                Dim handle As IntPtr = NativeMethods.ExtCreatePen((7 Or num), 1, New LOGBRUSH(0, ColorTranslator.ToWin32(pen.Color), 0), 2, New Integer() { 1, 1 })
                If (handle = IntPtr.Zero) Then
                    Throw New Win32Exception(Marshal.GetLastWin32Error)
                End If
                Me.hpen = New HandleRef(Me, handle)
            End Sub

            Private Sub System.IDisposable.Dispose() Implements IDisposable.Dispose
                If (Not Me.pen Is Nothing) Then
                    If (NativeMethods.DeleteObject(Me.hpen) = 0) Then
                        Dim exception As New Win32Exception
                        Dim message As String = exception.Message
                    End If
                    Me.pen = Nothing
                End If
            End Sub


            ' Properties
            Friend ReadOnly Property Handle As HandleRef
                Get
                    Return Me.hpen
                End Get
            End Property


            ' Fields
            Private hpen As HandleRef
            Private pen As Pen
        End Class

        Private Enum XpSchemeColorIndex
            ' Fields
            BkGnd = 1
            Border = 2
            FgGnd = 0
            Highlight = 3
            Shadow = 4
        End Enum

        Friend Enum XpThemeColorStyles
            ' Fields
            Blue = 0
            Green = 2
            Silver = 1
        End Enum
    End Class
End Namespace

